﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Timers;
using GSF;

namespace SIEGate.Core.Strawman
{
    public class DebuggingNetworkCommunicator : NetworkCommunicator
    {
        private bool m_running = false;
        private int m_count = 0;
        private Timer m_logtimer;

        public class NetworkCommunicatorInfo : NetworkCommunicatorInfoBase
        {
            public override void ExtractConfiguration(DataSet configurationTables)
            {
                base.ExtractConfiguration(configurationTables);
                if ((object)configurationTables != null && configurationTables.Tables.Contains("ActiveMeasurements"))
                {
                    try
                    {
                        IEnumerable<Guid> mysub;
                        DataRow[] filteredRows = configurationTables.Tables["ActiveMeasurements"].Select("Subscribed <> 0");
                        mysub = filteredRows.Select(r => r["SignalID"].ToNonNullString(Guid.Empty.ToString()).ConvertToType<Guid>());
                        IncomingStreamIDs.UnionWith(mysub);
                    }
                    catch (Exception ex)
                    {
                        LogDispatcher.Log(LogLevel.Error, "DebuggingNetworkCommunicator", "configuration", string.Format("Failed to define subscribed measurements: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace));

                    }
                }
                else
                {
                    LogDispatcher.E("DebuggingNetworkCommunicator", "configuration", "No Data tables presented!");
                }
            }
        }
        private void setup_timer()
        {
            m_logtimer = new Timer(1000);
            m_logtimer.AutoReset = true;
            m_logtimer.Elapsed += timer_tick;
        }
        private void end_timer()
        {
            m_logtimer.Enabled = false;
        }
        private void timer_tick(object sender, EventArgs args)
        {
            LogDispatcher.Log(LogLevel.Verbose, "DebugCommunicatior", "infocount", "Count of recived measuremnts: {0}", m_count);
        }

        public override void Register()
        {

        }

        public override void Start()
        {
            m_running = true;
            setup_timer();
            LogDispatcher.D("Fanciness", "start", "entering subscribe");
            MessageBusProxy.Subscribe(Info.IncomingStreamIDs);
            LogDispatcher.D("Fanciness", "start", "subscription finished, entering loop");
            while (m_running)
            {
                IMeasurement[] foo = MessageBusProxy.Recv();
                if (foo.Length > 0)
                    m_count += foo.Length;
                if (m_count > 0 && !m_logtimer.Enabled)
                {
                    m_logtimer.Enabled = true;
                }
            }
        }
        public override void Stop()
        {
            m_running = false;
            end_timer();
            MessageBusProxy.UnSubscribe(Info.IncomingStreamIDs);
        }
        public DebuggingNetworkCommunicator(IMessageBusProxy bus, NetworkCommunicatorInfo info)
            : base(bus, info)
        {
            // nothing fancy here!   
        }
    }
}
